package com.tjp.subject.common.entity;

import java.util.Collections;
import java.util.List;

/**
 * @program: jz-club
 * @description: 分页返回实体
 * @author: aaronTang
 * @create: 2024-03-01
 */
public class PageResult<T> {

    private Integer page = 1;

    private Integer pageSize = 10;

    private  Integer total = 0;

    private Integer totalPage = 0;

    private List<T> result = Collections.emptyList();

    private Integer start = 1;

    private Integer end = 0;

    public void setRecords(List<T> result) {
        this.result = result;
        if (result != null && result.size() > 0) {
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (this.pageSize > 0) {
            //取余数是怕数据总量有余数,需要额外的一页来承载
            this.totalPage = (total / this.pageSize) + (total % this.pageSize == 0 ? 0 : 1);
        }else {
            this.totalPage = 0;
        }

        //设置起始
        this.start = (this.page > 0 ? (this.page - 1) * this.pageSize : 0) + 1;
        //设置结束
        this.end = (this.start - 1 + this.pageSize * (this.page > 0 ? 1 : 0));
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null || pageSize.intValue() == 0) {
            return;
        }

        this.pageSize = pageSize;
    }

    public void setPage(Integer page) {
        if (page == null || page.intValue() == 0) {
            return;
        }
        this.page = page;
    }


}
